<template>
  <div>
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">Music</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
     <el-card>
  <el-row :gutter="20">
        <el-col :span="10">
          <el-input
          @clear="getList"
          v-model="queryInfo.query"
            placeholder="请输入内容" clearable >
            <el-button
            slot="append"
            @click="getList"
              icon="el-icon-search">
              </el-button> 
              </el-input>
          </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="adduser">添加用户</el-button>
        </el-col>
      </el-row>
       <!-- 表格 -->
     <el-table border
     stripe
     height="700px"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        type="index"
        
        width="50"
        >
      </el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        width="950">
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格"
        width="80">
      </el-table-column>
      <el-table-column
      width="80"
        prop="goods_weight"
        label="商品重量">
      </el-table-column>
      <el-table-column
      width="170"
        prop="add_time"
        label="创建时间">
        <template #default="{row}">{{ moment(row.add_time).format('YYYY-MM-DD HH:mm:ss') }}</template>
      </el-table-column>
      <el-table-column
       
        label="操作">
        <template #default="{row}">
          <el-button type="primary" icon="el-icon-edit" @click="editgood(row.goods_id)">编辑</el-button>
        <el-button type="danger"  icon="el-icon-delete" @click="delgood(row.goods_id)" >删除</el-button>
       
        </template>
      </el-table-column>
    </el-table>
     </el-card>
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagesize"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 修改对话框 -->
    <el-dialog
  title="提示"
  :visible.sync="edit"
  width="60%"
  >
  <el-form :model="editForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="商品名称" prop="goods_name">
    <el-input v-model="editForm.goods_name"></el-input>
  </el-form-item>
  <el-form-item label="价格" prop="goods_price">
    <el-input v-model="editForm.goods_price"></el-input>
  </el-form-item>
  <el-form-item label="重量" prop="goods_weight">
    <el-input v-model="editForm.goods_weight"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="qx">取 消</el-button>
    <el-button type="primary" @click="editInfo">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
import { Message } from 'element-ui'
import {getGoodsList,editGoodsList,editgoods,delGoodsList} from '@/api/user'
export default {
  methods: {
    adduser() {
      this.$router.push('goods/add')
    },
   async delgood(id) {
     let res =  await delGoodsList(id)
      if (res.meta.status !== 200) {
        Message.error('删除失败')
      } else {
        Message.success('删除成功')
      }
      this.getList()
    },
    qx() {
      console.log(1);
      this.edit = false
    },
   async editInfo() {
      this.edit = false
    let res =  await editgoods(this.editForm.goods_id,this.editForm,this.editForm.cat_id)
    console.log(res);
    },
    async editgood(id) {
    this.edit = true
      let res = await editGoodsList(id)
      console.log(res);
      this.editForm = res.data
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getList()

    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getList()
    },
    async getList() {
      
      let res = await getGoodsList(this.queryInfo)
      console.log(res);
      this.tableData = res.data.goods
      this.total = res.data.total
    }
  },
  created() {
    console.log(this);
    this.getList()
  },
  data() {
    return {
      editForm: {
        goods_price: '',
        goods_weight: '',
        goods_name:''
        
      },
      edit:false,
      total:0,
      moment,
      tableData: [],
        queryInfo: {
      query: '',
      pagenum: 1,
      pagesize:10
    },
    }
  },
}
</script>

<style>

</style>